package com.wxl.common.base

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.SkinAppCompatDelegateImpl
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.PopupInfo
import com.lxj.xpopup.interfaces.XPopupCallback
import com.tencent.bugly.beta.Beta
import com.wxl.common.R
import com.wxl.common.util.KeyBoardUtil
import com.wxl.common.util.LayoutHelper
import skin.support.annotation.Skinable


/**
 * create file time : 2021/3/1
 * create user : wxl
 * subscribe :
 */
@Skinable
abstract class AbsActivity : AppCompatActivity() {

    open var isCheckAppUpgrade = true

    open val popWindows = arrayListOf<BasePopupView>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setWindowStatusBar()

        if (addTitleSwitcher()) {
            setContentView(addTitleTopLayout())
        } else {
            setContentView(addTitleBgLayout())
        }

        onCreateViewChanged()
    }


    open fun setTopTitle(title: String) {
        findViewById<TextView>(R.id.titleTextView).text = title
    }

    open fun addTitleSwitcher(): Boolean {
        return true
    }

    open fun addTitleBgLayout(): View {
        return LayoutHelper.addTitleBgLayout(inflateLayout())
    }

    open fun addTitleTopLayout(): View {
        return LayoutHelper.addTitleTopLayout(inflateLayout())
    }

    open fun inflateLayout(): ViewGroup {
        return View.inflate(this, getCreateContentViewId(), null) as ViewGroup
    }


    fun addPop(pop: BasePopupView) {
        if (pop.popupInfo == null) {
            pop.popupInfo = PopupInfo()
        }
        pop.popupInfo.xPopupCallback = object : XPopupCallback {
            override fun onBackPressed(): Boolean {
                return false
            }

            override fun onDismiss() {
                popWindows.remove(pop)
            }

            override fun beforeShow() {

            }

            override fun onCreated() {

            }

            override fun onShow() {

            }

        }
        popWindows.add(pop)
    }


    override fun onDestroy() {
        super.onDestroy()
        dismissClearAllPop()
    }

    fun dismissClearAllPop(){
        popWindows.forEach {
            it.dismiss()
        }
        popWindows.clear()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (popWindows.isNotEmpty()) {
            onKeyDownPop(keyCode, event)
        } else {
            onKeyDownEvent(keyCode,event)
        }
    }

    private fun onKeyDownPop(keyCode: Int, event: KeyEvent?) : Boolean{
      return  popWindows[popWindows.size - 1].onKeyDown(keyCode, event)
    }

    open fun onKeyDownEvent(keyCode: Int,event: KeyEvent?) : Boolean{
        return false
    }

    abstract fun getCreateContentViewId(): Int


    abstract fun onCreateViewChanged()


    internal open fun setWindowStatusBar() {
        //状态栏透明化: 侵入式透明status bar
        //状态栏透明化: 侵入式透明status bar

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val window: Window = window
            // Translucent status bar
            window.setFlags(
                View.SYSTEM_UI_FLAG_FULLSCREEN,
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            )
        }

        window.statusBarColor = Color.TRANSPARENT
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        KeyBoardUtil.hideSoftInput(window.decorView)
        KeyBoardUtil.releaseObserver(this)
    }

    @NonNull
    override fun getDelegate(): AppCompatDelegate {
        return SkinAppCompatDelegateImpl.get(this, this)
    }

    override fun onResume() {
        super.onResume()
        if (isCheckAppUpgrade) {
            Beta.checkUpgrade(false, true)
        }
    }
}